package com.papercut.purchase;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.client.Constants;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.papercut.R;
import com.papercut.gui.ICallback;
import com.papercut.gui.SwipeCheck;
import com.papercut.service.PaperCutDatabase;
import com.papercut.service.RequestWorker;
import com.papercut.service.RestService;
import com.xtify.android.sdk.NotificationActivity;

/**
 * This is a sample Activity that responds to a custom notification action. The
 * data is retrieved from the Intent and in this case, it is displayed in a text
 * box. To launch this Activity, your notification would set the action to
 * "com.acmelabs.DISPLAY_CUSTOM_DATA" in order to match the intent filter
 * defined in AndroidManifest.xml.
 */
public class PurchaseActivity extends Activity {
	private RestService mBoundService;
	private ServiceConnection connection;
	private Intent intent;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Intent intent = new Intent(this, com.papercut.service.RestService.class);
		this.connection = new ServiceConnection() {

			public void onServiceConnected(ComponentName name, IBinder service) {
				mBoundService = ((RestService.LocalBinder) service)
						.getService();

				bootstrap();
	//			setContentView(R.layout.purchase_list_container);

			}

			public void onServiceDisconnected(ComponentName name) {
				Log.i("Activity", "on disconnect" + name.toString());

			}

		};
		bindService(intent, connection, Context.BIND_AUTO_CREATE);
		this.intent = getIntent();

			
			
		

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		PaperCutDatabase db = new PaperCutDatabase(this);
		SharedPreferences prefs = getSharedPreferences(
				"storage", 0);
		boolean a = prefs.getBoolean("YAY", false);
		
		if (a) {
			String transactionIDR = intent.getStringExtra("TRANS");
			db.updateTransaction(Integer.parseInt(transactionIDR));
		}
		finish();
		
	}
	
	
	private void bootstrap() {
		final LinearLayout linear_view = new LinearLayout(PurchaseActivity.this);
		linear_view.setOrientation(LinearLayout.VERTICAL);
		linear_view.setBackgroundResource(R.drawable.amazon_background_swipecheck);
		final ListView view = (ListView) findViewById(R.id.purchaseList);

		ICallback callback = new ICallback() {

			@Override
			public void success(Object object) {

				PurchaseEntity entity = (PurchaseEntity) object;
				//PaperCutDatabase db = new PaperCutDatabase(PurchaseActivity.this);
				
			//	db.newNotification(decodeUriData(currIntent.getData()), entity.getMerchant(), entity.getDate(), 2000);

				List<PurchaseItem> items = entity.getItems();
//				List<PurchaseItem> items = new ArrayList<PurchaseItem>();
//				items.add(new PurchaseItem("ASDF",234));
//				items.add(new PurchaseItem("ASasdfasdfDF",8));
//				items.add(new PurchaseItem("234",345));
//				items.add(new PurchaseItem("23423",4));
//				items.add(new PurchaseItem("ASasd232afasdfDF",235));
//				items.add(new PurchaseItem("ASasdsadffasdfDF",56));
//				items.add(new PurchaseItem("asd",345));
//				items.add(new PurchaseItem("ASDF",234));
//				items.add(new PurchaseItem("ASasdfasdfDF",90));
//				items.add(new PurchaseItem("234",345));
//				items.add(new PurchaseItem("23423",4));
//				items.add(new PurchaseItem("ASasd232afasdfDF",235));
//				items.add(new PurchaseItem("ASasdsadffasdfDF",56));
//				items.add(new PurchaseItem("asd",345));
//				items.add(new PurchaseItem("ASDF",234));
//				items.add(new PurchaseItem("ASasdfasdfDF",98));
//				items.add(new PurchaseItem("234",345));
//				items.add(new PurchaseItem("23423",4));
//				items.add(new PurchaseItem("ASasd232afasdfDF",235));
//				items.add(new PurchaseItem("ASasdsadffasdfDF",56));
//				items.add(new PurchaseItem("asd",345));


			//	view.setAdapter(new PurchaseListAdapter(getLayoutInflater(),
			//			items));
				
				TextView app_title = new TextView(PurchaseActivity.this);
				LinearLayout.LayoutParams app_title_layout = new LinearLayout.LayoutParams(
							LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				
				app_title_layout.setMargins(0, 20, 0, 20);
				app_title.setText(entity.getMerchant());
				app_title.setTextSize(24);
				app_title.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
				app_title.setTextColor(Color.rgb(231, 122, 0));
				app_title.setGravity(Gravity.CENTER_HORIZONTAL);
				linear_view.addView(app_title, app_title_layout);
				
				ScrollView scroll_purchase = new ScrollView(PurchaseActivity.this);
				ViewGroup.LayoutParams scroll_layout = new ViewGroup.LayoutParams(480,400);

				LinearLayout scroll_linear = new LinearLayout(PurchaseActivity.this);
				scroll_linear.setOrientation(LinearLayout.VERTICAL);

				double price_num = 0;
				for (PurchaseItem item : items) {
					TextView text = new TextView(PurchaseActivity.this);		
					text.setBackgroundColor(Color.rgb(240, 249, 255));
					LinearLayout.LayoutParams text_layout = new LinearLayout.LayoutParams(
								LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
					
					text_layout.setMargins(15, 0, 15, 0);
					text.setText(Html.fromHtml("<font color=#001870>" + "Title: " + "</font> <font color=#E77A00>" + item.getTitle() + "</font>" + "<br />" +
							"<font color=#001870>" + "Price: " + "</font> <font color=#E77A00>" + item.getPrice() + "</font>"));
					
					text.setTextSize(16);
					text.setGravity(Gravity.LEFT);
					scroll_linear.addView(text, text_layout);
					
					ImageView line = new ImageView(PurchaseActivity.this);
					LinearLayout.LayoutParams line_layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					line_layout.setMargins(15, 0, 15, 0);
					line.setImageResource(R.drawable.in_between_line);
					scroll_linear.addView(line, line_layout);	
					price_num += item.getPrice();
				}
				
				scroll_purchase.addView(scroll_linear);
				
				linear_view.addView(scroll_purchase, scroll_layout);
				
				TextView price = new TextView(PurchaseActivity.this);
				LinearLayout.LayoutParams price_layout = new LinearLayout.LayoutParams(
							LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				
				price_layout.setMargins(15, 20, 0, 20);
				double shipping = 1.01;
				double tax = .1*price_num;
				double total = price_num + shipping + tax;
				price.setText(Html.fromHtml("<font color=#001870>" + "Total Price: &nbsp;" + "</font> <font color=#E77A00>" + Double.toString(price_num) + "</font>" + "<br />" +
						"<font color=#001870>" + "Shipping: &nbsp;" + "</font> <font color=#E77A00>" + "1.01" + "</font>" + "<br />" +
						"<font color=#001870>" + "Tax (10%): &nbsp;" + "</font> <font color=#E77A00>" + Double.toString(tax) + "</font>" + "<br />" + 
						"<font color=#001870>" + "Grand Total: &nbsp;" + "</font> <font color=#E77A00>" + Double.toString(total) + "</font>"));
				price.setTextSize(18);
			//	price.setTypeface(Typeface.DEFAULT, Typeface.);
			//	price.setTextColor(Color.rgb(231, 122, 0));
				price.setGravity(Gravity.LEFT);
				linear_view.addView(price, price_layout);
				
				LinearLayout horizontal = new LinearLayout(PurchaseActivity.this);
				horizontal.setOrientation(LinearLayout.HORIZONTAL);
				
				ImageView small_save = new ImageView(PurchaseActivity.this);
				LinearLayout.LayoutParams small_save_layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				small_save_layout.setMargins(42, 25, 15, 0);
				small_save.setImageResource(R.drawable.small_save);
				small_save.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						
						SharedPreferences prefs = getSharedPreferences(
								"storage", 0);
						Editor editor = prefs.edit();
						editor.putBoolean("VERIFYTHING", true);
						editor.commit();
						Intent intent = new Intent(PurchaseActivity.this, SwipeCheck.class);
						startActivityForResult(intent, 40);
					
					}
					
				});
				horizontal.addView(small_save, small_save_layout);

				ImageView cancel = new ImageView(PurchaseActivity.this);
				LinearLayout.LayoutParams cancel_layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				cancel_layout.setMargins(15, 25, 15, 0);
				cancel.setImageResource(R.drawable.cancel_small);
				cancel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						CharSequence text = "Canceled Verification";
						int duration = Toast.LENGTH_SHORT;

						Toast toast = Toast.makeText(PurchaseActivity.this, text, duration);
						toast.show();
						
						finish();
					}
					
				});
				
				horizontal.addView(cancel, cancel_layout);

				linear_view.addView(horizontal);


				setContentView(linear_view);
				
			}

			@Override
			public void failure(String error) {

			}

		};

		Thread transactionThread = new Thread(new RequestWorker(callback) {

			@Override
			public void run() {

				String transactionIDR = intent.getStringExtra("TRANS");
        
		
				int transactionID = Integer.parseInt(transactionIDR);
				PurchaseEntity pe = mBoundService.getTranscation(transactionID);

				callback.success(pe);
				
			}

		});
		transactionThread.run();

	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		unbindService(connection);
	}
	
	

};